package com.procurement.orders.controller;

import java.util.List;
import java.io.IOException;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.procurement.orders.domain.PcmMaterialRecord;
import com.ruoyi.common.core.constant.SecurityConstants;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.RenotePcmService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.procurement.orders.domain.PcmPurchaseRequest;
import com.procurement.orders.service.IPcmPurchaseRequestService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 采购申请Controller
 * 
 * @author liyang
 * @date 2023-12-05
 */
@RestController
@RequestMapping("/request")
public class PcmPurchaseRequestController extends BaseController
{
    @Autowired
    private IPcmPurchaseRequestService pcmPurchaseRequestService;

    @Resource
    private RenotePcmService renotePcmService;

    /**
     * 查询物料列表
     */
    @RequiresPermissions("orders:request:listing")
    @GetMapping("/listing")
    public TableDataInfo RecordList()
    {
        startPage();
        List<PcmMaterialRecord> list = pcmPurchaseRequestService.RecordList();
        return getDataTable(list);
    }


    /**
     * 查询采购申请列表
     */
    @RequiresPermissions("orders:request:list")
    @GetMapping("/list")
    public TableDataInfo list(PcmPurchaseRequest pcmPurchaseRequest)
    {
        startPage();
        List<PcmPurchaseRequest> list = pcmPurchaseRequestService.selectPcmPurchaseRequestList(pcmPurchaseRequest);
        return getDataTable(list);
    }

    /**
     * 导出采购申请列表
     */
    @RequiresPermissions("orders:request:export")
    @Log(title = "采购申请", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PcmPurchaseRequest pcmPurchaseRequest)
    {
        List<PcmPurchaseRequest> list = pcmPurchaseRequestService.selectPcmPurchaseRequestList(pcmPurchaseRequest);
        ExcelUtil<PcmPurchaseRequest> util = new ExcelUtil<PcmPurchaseRequest>(PcmPurchaseRequest.class);
        util.exportExcel(response, list, "采购申请数据");
    }

    /**
     * 获取采购申请详细信息
     */
    @RequiresPermissions("orders:request:query")
    @GetMapping(value = "/{purchaseRequestNumber}")
    public AjaxResult getInfo(@PathVariable("purchaseRequestNumber") String purchaseRequestNumber)
    {
        System.out.println(purchaseRequestNumber);
        return success(pcmPurchaseRequestService.selectPcmPurchaseRequestByPurchaseRequestNumber(purchaseRequestNumber));
    }

    /**
     * 新增采购申请
     */
    @RequiresPermissions("orders:request:add")
    @Log(title = "采购申请", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PcmPurchaseRequest pcmPurchaseRequest)
    {
        pcmPurchaseRequest.setPurchaseRequestNumber(renotePcmService.scbm("CG", SecurityConstants.INNER));
        pcmPurchaseRequest.setSubmitter(SecurityUtils.getUserId());
        return toAjax(pcmPurchaseRequestService.insertPcmPurchaseRequest(pcmPurchaseRequest));
    }

    /**
     * 修改采购申请
     */
    @RequiresPermissions("orders:request:edit")
    @Log(title = "采购申请", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PcmPurchaseRequest pcmPurchaseRequest)
    {
        return toAjax(pcmPurchaseRequestService.updatePcmPurchaseRequest(pcmPurchaseRequest));
    }

    /**
     * 删除采购申请
     */
    @RequiresPermissions("orders:request:remove")
    @Log(title = "采购申请", businessType = BusinessType.DELETE)
	@DeleteMapping("/{purchaseRequestNumbers}")
    public AjaxResult remove(@PathVariable String[] purchaseRequestNumbers)
    {
        return toAjax(pcmPurchaseRequestService.deletePcmPurchaseRequestByPurchaseRequestNumbers(purchaseRequestNumbers));
    }
}
